﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Computer_Manager.BUS;
using Computer_Manager.DTO;

namespace Computer_Manager.GUI
{
    public partial class ImportExcel_Cus : DevExpress.XtraEditors.XtraForm
    {
        BUS_customer bus_cus = new BUS_customer();
        DTO_customer dto_cus = new DTO_customer();
        frmCustomer obj = (frmCustomer)Application.OpenForms["frmCustomer"];
        public ImportExcel_Cus()
        {
            InitializeComponent();
        }
        private void btnOpenExcel_Click(object sender, EventArgs e)
        {
            OpenFileDialog mo = new OpenFileDialog();
            mo.Filter = "(*.xls)|*.xls";
            System.Data.DataTable bang = new DataTable();
            if (mo.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(mo.FileName))
                {
                    txtDuongdan.Text = mo.FileName;
                    string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + txtDuongdan.Text + ";" + "Extended Properties=Excel 8.0";
                    OleDbConnection conn = new OleDbConnection(PathConn);
                    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn);
                    DataTable dt = new DataTable();
                    myDataAdapter.Fill(dt);
                    gridControl1.DataSource = dt;
                }
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= gridView1.RowCount - 1; i++)
            {
                try
                {
                    dto_cus.name_cus = gridView1.GetDataRow(i)[0].ToString();
                    dto_cus.address = gridView1.GetDataRow(i)[1].ToString();
                    dto_cus.phone = gridView1.GetDataRow(i)[2].ToString();
                    dto_cus.email = gridView1.GetDataRow(i)[3].ToString();
                    dto_cus.type = true;
                    bus_cus.customer_insert(dto_cus);
                        //XtraMessageBox.Show("Import thành công");
                    
                }
                catch (Exception){}
            }
            this.Close();
            MessageBox.Show("Import thàng công!!!");
            obj.datagridview();
        }

    }
}